package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.CmsSubjectPage;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.CmsSubject;
import com.genesis.mall.service.ICmsSubjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 专题表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/subject")
public class CmsSubjectController {

    @Autowired
    private ICmsSubjectService cmsSubjectService;
    /**
     * 新品新增商品展示
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public Result<CmsSubjectPage<CmsSubject>> list(Long pageNum, Long pageSize, String keyword){
        Page<CmsSubject> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<CmsSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(keyword),CmsSubject::getTitle,keyword);
        queryWrapper.orderByDesc(CmsSubject::getCreateTime);

        cmsSubjectService.page(pageInfo,queryWrapper);

        CmsSubjectPage<CmsSubject> cmsSubjectPage = new CmsSubjectPage<>();
        BeanUtils.copyProperties(pageInfo,cmsSubjectPage);

        return Result.success(cmsSubjectPage);
    }

    @GetMapping("/listAll")
    public Result<List<CmsSubject>> listAll(){
        LambdaQueryWrapper<CmsSubject> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CmsSubject::getShowStatus,1);
        List<CmsSubject> subjects = cmsSubjectService.list(lqw);
        return Result.success(subjects);
    }
}
